<template>
  <div>
    <el-dialog
      :before-close="cancel"
      :title="info.isAdd?'添加角色':'编辑角色'"
      :visible.sync="info.isShow"
      center
    >
      <el-form ref="role" :model="roleInfo" :rules="rules">
        <el-form-item prop="rolename" label="角色名称" :label-width="formLabelWidth">
          <el-input v-model="roleInfo.rolename" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="rolename" label="角色权限" :label-width="formLabelWidth">
          <el-tree
            ref="tree"
            :data="menulist"
            show-checkbox  
            node-key="id"
            default-expand-all
            :props="{
                children:'children',
                label:'title'
                }"
            check-strictly
          ></el-tree>
        </el-form-item>

        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch
            v-model="roleInfo.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button @click="confirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { roleadd, roleedit, roleInfo } from "../../request/api";
import { mapGetters, mapActions } from "vuex";
export default {
  data() {
    return {
      roleInfo: {
        rolename: "",
        menus: "",
        status: 1
      },
      formLabelWidth: "120px",
      rules: {
        rolename: [
          { required: true, message: "请输入角色名称", trigger: "blur" },
          { min: 2, max: 8, message: "长度在 2 到 8个字符", trigger: "blur" }
        ]
      },
      defaultProps: {
        children: "children",
        label: "label"
      }
    };
  },
  props: ["info"],
  mounted() {
    if (this.menulist.length == 0) {
      this.getMenu();
    }
  },
  computed: {
    ...mapGetters({
      menulist: "menu/getMenu"
    })
  },
  methods: {
    ...mapActions({
      getRole: "role/getRoleActions",
      getMenu: "menu/getMenuActions"
    }),
    cancel() {
      this.$emit("cancel", false);
      this.reset();
    },
    //清空输入的表单内容以及相关验证
    reset() {
      this.roleInfo = {
        rolename: "",
        menus: "",
        staus: 1
      };
      this.$refs.role.resetFields();
      //清空tree节点的选择状态
      this.$refs.tree.setCheckedKeys([]);
    },
    //创建一个查询一条数据事件
    lookup(id) {
      roleInfo({ id }).then(res => {
        console.log(res, "角色");
        if (res.code == 200) {
          this.roleInfo = res.list;
          this.roleInfo.id = id;
          //给tree节点设置数据 返回的数据格式是字符串，我们要把它转化成数组
          this.$refs.tree.setCheckedKeys(this.roleInfo.menus.split(","));
        }
      });
    },
    //封装一个确定事件
    confirm() {
      this.$refs.role.validate(val => {
        if (val) {
          console.log(this.roleInfo);
          // console.log(this.$refs.tree.getCheckedKeys().join(','),'获取选中的id组成的数组');
          this.roleInfo.menus = this.$refs.tree.getCheckedKeys().join(",");

          //验证成功之后，分别调用添加或者编辑接口
          if (this.info.isAdd) {
            roleadd(this.roleInfo).then(res => {
              // console.log(res,'添加结果')
              if (res.code == 200) {
                this.$message.success(res.msg);
                this.cancel();
                //调用列表接口
                this.getRole();
              } else {
                this.$message.error(res.msg);
              }
            });
          } else {
            roleedit(this.roleInfo).then(res => {
              // console.log(res,'添加结果')
              if (res.code == 200) {
                this.$message.success(res.msg);
                this.cancel();
                //调用列表接口
                this.getRole();
              } else {
                this.$message.error(res.msg);
              }
            });
          }
        } else {
          console.log("验证失败");
        }
      });
    }
  }
};
</script>

<style  scoped>
</style>
